package com.wxb.quartz.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

/**
 * @author Feng Yu
 * @version V1.0
 * @date 2017-05-08
 */
public class GenericResponse {

    public static String buildData(Object data){
        return buildMessage(GenericCode.success_code, "success", "rtnObj", data);
    }

    public static String buildData(String key, Object data){
        JSONObject rtnobj = new JSONObject(1);
        rtnobj.put(key, data);
        return buildData(rtnobj);
    }

    public static String buildTotal(int total){
        JSONObject rtnobj = new JSONObject(1);
        rtnobj.put("total", total);
        return buildData(rtnobj);
    }

    public static String buildPageList(Object list){
        JSONObject rtnobj = new JSONObject(1);
        rtnobj.put("list", list);
        return buildData(rtnobj);
    }

    public static String buildPageSearch(int total, Object list){
        JSONObject rtnobj = new JSONObject(2,true);
        rtnobj.put("total", total);
        rtnobj.put("list", list);
        return buildData(rtnobj);
    }

    public static String buildError(String errCode, String errMsg){
        return buildMessage(errCode, errMsg, null, null);
    }

    public static String buildMessage(){
        return buildMessage(GenericCode.success_code, "success",null, null);
    }

    public static String buildMessage(String beanName, Object rtnObjData){
        return buildMessage(GenericCode.success_code, "success",beanName, rtnObjData);
    }

    public static String buildMessage(String rtnCode, String rtnMsg, String beanName, Object rtnObjData){

        JSONObject jsonObject = new JSONObject(3,true);
        jsonObject.put("rtnCode", rtnCode);
        jsonObject.put("rtnMsg", rtnMsg);
        if(StringUtils.hasText(beanName)){
            jsonObject.put(beanName, rtnObjData);
        }
        return jsonObject.toJSONString();
    }
}
